﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using BrokerBase.Lib.Query;
using SaleLibrary.License;
using BrokerBase.Lib.Core;
using BrokerBase.Lib.Client;

namespace Broker.Mgr.SaleView
{
    /// <summary>
    /// Interaction logic for ActiveCodeListPage.xaml
    /// </summary>
    public partial class ActiveCodeListPage
    {
        private const int PAGE_SIZE = 15;

        private PagingQuery pageingQuery;

        private string hqlBase = "from ActiveCode as a ";//默认hql


        public ActiveCodeListPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ActiveCodeListPage_Loaded);
        }

        void ActiveCodeListPage_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            ActiveCode ac = ActiveCode.CreateActiveCode(EditionType.Standard, 12, 30);

            ActiveCodeForm frm = new ActiveCodeForm(ac);
            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.Owner = AppGlobal.MinWin;
            frm.ShowDialog();
        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            View();
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            ActiveCode ac = gvActiveCode.SelectedItem as ActiveCode;

            if (ac != null)
            {

                if (ac.IsSend)
                {
                    MessageBox.Show("激活码已经发送，不能删除!", "提示");
                    return;

                }

                ActiveCodeForm frm = new ActiveCodeForm(ac);

                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = AppGlobal.MinWin;
                frm.ShowDialog();
            }
        }


        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            BuildQuery();
            Query();
        }

        private void gvActiveCode_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            View();
        }

        private void dpList_PageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            if (pageingQuery != null)
            {
                pageingQuery.FirstResult = e.NewPageIndex * PAGE_SIZE;
                Query();
            }
        }

        private void BuildQuery()
        {
            pageingQuery = new PagingQuery();
            pageingQuery.MaxResults = PAGE_SIZE;

            string where = "where 1=1";

            if (!string.IsNullOrEmpty(txtCode.Text))
            {
                where += string.Format(" and a.Code Like '%{0}%'",txtCode.Text.Trim());
            }

            if (dpSendStart.SelectedDate != null)
            {
                where += string.Format(" and exists (from ActiveCodeSend as s where s.ActiveCode.Id=a.Id and  s.SendTime>='{0}')", dpSendStart.SelectedDate.Value.ToString("yyyy-MM-dd"));
            }

            if (dpSendEnd.SelectedDate != null)
            {
                where += string.Format(" and exists (from ActiveCodeSend as s where s.ActiveCode.Id=a.Id and  s.SendTime<='{0}')", dpSendEnd.SelectedDate.Value.ToString("yyyy-MM-dd"));
            }

            if (!string.IsNullOrEmpty(txtReger.Text))
            {
                where += string.Format(" and a.Registrant.Name Like :regName ", txtReger.Text.Trim());
                pageingQuery.AddParameter("regName", "string", string.Format("%{0}%", txtReger.Text.Trim()));
            }

            if (dpRegStart.SelectedDate != null)
            {
                where += string.Format(" and exists (from ActiveCodeReg as r where r.ActiveCode.Id=a.Id and  r.RegTime>='{0}')", dpRegStart.SelectedDate.Value.ToString("yyyy-MM-dd"));
            }

            if (dpRegEnd.SelectedDate != null)
            {
                where += string.Format(" and exists (from ActiveCodeReg as r where r.ActiveCode.Id=a.Id and  r.RegTime<='{0}')", dpRegEnd.SelectedDate.Value.ToString("yyyy-MM-dd"));
            }

            pageingQuery.HQL = hqlBase + where +" order by Id desc";
            pageingQuery.TotalHQL = "select count(*) " + hqlBase + where;
        }

        protected void Query()
        {
            try
            {
                busyRoot.IsBusy = true;
                busyRoot.BusyContent = "正在加载数据...";

                IBaseService<ActiveCode> svr = new BaseService<ActiveCode>(AppGlobal.HbmSessionFactory.GetCurrentSession());

                long total = 0;
                IList<ActiveCode> ls = svr.GetListByHQLWithPaging(pageingQuery, out total);

                int t3 = Environment.TickCount;


                dpList.ItemCount = (int)total;
                gvActiveCode.ItemsSource = ls;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                busyRoot.IsBusy = false;
            }

        }


        private void View()
        {
            ActiveCode ac = gvActiveCode.SelectedItem as ActiveCode;

            if (ac != null)
            {
                ActiveCodeForm frm = new ActiveCodeForm(ac);

                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = AppGlobal.MinWin;
                frm.ShowDialog();
            }
        }



    }
}
